package ch.usi.inf.pf2.cell;

/**
 * This class contains the definition of possible data that could be stored in a
 * cell.
 * 
 * @author Lorenzo Baracchi
 */
public abstract class Data
{
    /**
     * Return a string representation of the Data object
     * 
     * @return the string
     */
    public abstract String toString();

    /**
     * Compare this Data to the given data.
     * 
     * @param d
     *            the data to compare
     * @return true if the Data are equals, false otherwise
     */
    public abstract boolean equals(Data d);

    /**
     * Create an object Data that is actually an instance of one of the three
     * subclasses Value Formula and Text depending on the given parameter
     * contents. If contents is a number it creates a Value If contents is a
     * formula (start with the = sign) it creates a Formula If contents is a
     * text it creates a Text
     * 
     * @param contents
     *            is the string inserted by the user
     * @return an instance of the three subclasses Value Formula and Text
     */
    public static Data createData(String contents)
    {
	   try
	   {
		  double number = Double.parseDouble(contents);
		  return new Value(number); // the contents is a number
	   } catch (NumberFormatException e)
	   {
		  // the contents is not a number
		  if (contents.startsWith("="))
			 return new Formula(contents); // the contents is a Formula
		  else
			 return new Text(contents); // the contents is just text
	   }
    }

    /**
     * Return the instance of the Data object
     * 
     * @return a string containing the instance
     */
    public abstract String getInstance();

}